package com.chap17.completablefuture;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * <p>
 * <code>Demo04</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-05-09 9:51
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
@Slf4j
public class Demo06 {
    public static void main(String[] args) {

        ThreadPoolExecutor executor =
                new ThreadPoolExecutor(10,200,30,
                        TimeUnit.SECONDS,new ArrayBlockingQueue<>(200), new ThreadFactory() {
                    @Override
                    public Thread newThread(Runnable r) {
                        Thread thread = new Thread(r,"就餐线程池");
                        return thread;
                    }
                });
        log.debug("monkey进入餐厅，点了份西红柿炒番茄");
        CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
            log.debug("厨师炒菜");
            sleep(2, TimeUnit.SECONDS);
            return "西红柿炒番茄好了";
        },executor)//有菜没有饭，需要服务打饭,需要上一步任务的结果，然后饭菜一起吃
                .thenApply(dish -> {
                    log.debug("服务员打饭");
                    sleep(1, TimeUnit.SECONDS);
                    return dish + ",饭打好了";
                });


        log.debug("monkey在刷抖音");
        log.debug("{},monkey开吃",cf.join());
    }

    private static void sleep(int i, TimeUnit seconds) {

        try {
            seconds.sleep(i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
